﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TSCC.WinControl
{
    public partial class ucCSRInfo : ControlMatrix //UserControl
    {

        protected string logDate = string.Empty;

        public ucCSRInfo()
        {
            InitializeComponent();
            accessControl = txtToday;
        }

        public void SetUserInfo(TSCC.Interface.iUser p_usr)
        {
            //ucDate.Caption = System.DateTime.Today.ToShortDateString();
            txtToday.Text = getDate();
            ucCSRName.Caption = p_usr.GetUserName();
            ucCSRNo.Caption = p_usr.GetEmployeeNo();
            ucCSRTeam.Caption = p_usr.GetTeam();
        }

        public string GetToday()
        {
            //return ucDate.Caption;
            return txtToday.Text;
        }

        /// <summary>
        /// 取回YYYY-MM-DD 日期格式
        /// </summary>
        /// <param name="p">傳空字串即可</param>
        /// <returns></returns>
        public string GetToday(string p)
        {
            if (isDate(txtToday.Text))
            {
                return logDate;
            }
            return string.Empty;
        }

        protected string getDate()
        {
            string strY, strM, strD;
            strY = DateTime.Today.Year.ToString();
            strM = DateTime.Today.Month.ToString();
            strD = DateTime.Today.Day.ToString();
            if (strM.Length == 1)
            {
                strM = "0" + strM;
            }
            if (strD.Length == 1)
            {
                strD = "0" + strD;
            }
            return string.Format("{0}{1}{2}",strY,strM,strD);
        }

        private void txtToday_Leave(object sender, EventArgs e)
        {
            if (txtToday.Text.Equals(getDate()))
            {
                return;
            }
            if((!isDate(txtToday.Text)) & (this.Enabled == true))
            {
                MessageBox.Show("日期輸入錯誤！格式為YYYYMMDD");
                txtToday.Focus();
                return;
            }
            
        }

        //protected override void OnEnabledChanged(EventArgs e)
        //{
        //    base.OnEnabledChanged(e);            
        //}

        public override void ClearText()
        {
            base.ClearText();
            txtToday.Text = getDate();
        }


        bool isDate(string p_date)
        {
            string str;
            
            bool ok = true;
            try
            {
                str = string.Format("{0}-{1}-{2}"
                , p_date.Substring(0, 4)
                , p_date.Substring(4, 2)
                , p_date.Substring(6, 2));
                Convert.ToDateTime(str);
                logDate = str;
            }
            catch(Exception ex)
            {
                ok = false;
            }
            return ok;
        }
    }
}
